-- 船长小屋

function initCaptainCabin(self, buildingInfo, dbase, pos)
    if type(dbase) ~= "table" or type(dbase["need_property"]) ~= "number" then
        -- 没要求，配置错误
        error("获取不到船长小屋物品配置！");
        return;
    end

    -- 捕捉灵魂
    local function takeSoul()
        -- 尝试触发事件
        DungeonActionM.go("slime_event_soul", pos);

        -- 飘字
        alert(getLocStr("damnation_talk"));

        -- 建筑消失
        playBuildingDisappear(self);
    end

    -- 获得幽灵船长冈布奥
    local function okFun()
        local bonusArgs = SlimeEventM.query(113, "bonus_args");
        local cost = bonusArgs["cost"][1];
        if cost[1] ~= 1 then
            -- 配置错误
            error("船长小屋扣除配置错误！");
            return;
        end

        if ItemM.getAmount(ME.user, cost[2]) < cost[3] then
            -- 没有足够水晶
            alert(getLocStr("ghost_no_crystal_tip"));
            return;
        end

        -- 尝试尝试触发
        DungeonActionM.go("slime_event", pos, 113);

        -- 建筑消失
        playBuildingDisappear(self);
    end

    -- 获得船长碎片
    local function okFun2()
        local bonusArgs = SlimeEventM.query(114, "bonus_args");
        local cost = bonusArgs["cost"][1];
        if cost[1] ~= 1 then
            -- 配置错误
            error("船长小屋扣除配置错误！");
            return;
        end

        if ItemM.getAmount(ME.user, cost[2]) < cost[3] then
            -- 没有足够水晶
            alert(getLocStr("ghost_no_crystal_tip"));
            return;
        end

        -- 尝试尝试触发
        DungeonActionM.go("slime_event", pos, 114);

        -- 建筑消失
        playBuildingDisappear(self);
    end

    local petId = SlimeEventM.query(113, "dbase")["pet_id"];
    local bonusArgs = SlimeEventM.query(113, "bonus_args");
    local costNeed = bonusArgs["cost"][1];
    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 格子失效了
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            local props = PropM.fetchProps(ME.user, "rummage_soul");
            if #props > 0 and ItemM.getAmount(ME.user, costNeed[2]) < costNeed[3] then
                -- 水晶不足，有异界游侠称号
                self:openBuildingView2(takeSoul, nil, getLocStr("take_soul"), buildingInfo["view_text2"],
                    buildingInfo["dialog2"], nil, true);
            elseif ItemM.getAmount(ME.user, costNeed[2]) <= 0 then
                -- 没有水晶
                self:openBuildingView(nil, 0, buildingInfo["dialog2"], buildingInfo["view_text2"], nil, nil, nil, true);
            else
                -- 有水晶
                if PetM.getMyPet(petId) then
                    -- 有幽灵船长冈布奥
                    self:openBuildingView2(okFun2, nil, getLocStr("btn_text_exhcange"), buildingInfo["view_text2"],
                        getLocStr("ghost_master_slime_talk"), nil, true);
                else
                    -- 没有幽灵船长冈布奥
                    self:openBuildingView2(okFun, nil, buildingInfo["view_text"], buildingInfo["view_text2"],
                        buildingInfo["dialog1"], nil, true);
                end
            end
            AudioM.playFx("dup_shop");
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.icon:addTouchEventListener(onClicked);
end

